#include "StdAfx.h"
#include "FileControl.h"
#include <fstream>
#include <string.h>
using namespace std;


CFileControl::CFileControl(void)
{
}

CFileControl::~CFileControl(void)
{
}

void CFileControl::Save(CString filename, Node* root)
{
	ofstream fout(filename);

	if( !fout )
	{
		CString msg("Can't save the file!!");
		AfxMessageBox(msg);
	}

	SaveAllNode(root, fout);
		
}

void CFileControl::SaveAllNode(Node* node, ofstream &fout)
{
	if( NULL == node)
		return ;

	vector<Node*> *childList = node->GetChildList();
	CString inputData;
	inputData.Format(node->getData());
	inputData.Trim();
	fout << inputData;
	fout << ",";
	fout << (int)childList->size();
	fout << ",";

	for( vector<Node*>::iterator iter = childList->begin() ; iter != childList->end() ; iter++ )
	{
		SaveAllNode(*iter, fout);
	}
}

Node* CFileControl::Load(CString filename)
{
	CFile file;

	if( !file.Open(filename, CFile::modeRead))
	{
		AfxMessageBox("Can't Open the file");
		return NULL;
	}

	char buff[10240];
	memset(buff, 0, sizeof(char)* 10240);
	file.Read(buff, file.GetLength());

	vector<CString> data;
	vector<CString> childCount;
	
	CString temp;
	temp = strtok(buff, ",");
	data.push_back(temp);
	temp = strtok(NULL, ",");
	childCount.push_back(temp);

	while( strcmp("", (temp = strtok(NULL, ","))) )
	{
		data.push_back(temp);
		temp = strtok(NULL, ",");
		childCount.push_back(temp);
	}



	Node* root = NULL;


	return root;
}

void CFileControl::LoadAllNode(Node* parents, CFile &file)
{

}